package com.github.McPlugin.vexview;

import com.github.McPlugin.main;
import com.github.McPlugin.sql.*;
import com.github.McPlugin.util.Goods;
import com.github.McPlugin.util.SavaItemStack;
import lk.vexview.gui.VexGui;
import lk.vexview.gui.components.*;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import com.github.McPlugin.util.Task.*;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.scheduler.BukkitRunnable;

import java.time.LocalDate;
import java.util.*;

public class MyGui {

    //签到界面
    public static VexGui loginGui(Player player){
        //初始的信息
        int totalTime = 0;
        int monthTime = 0;
        String monthRecord = "0000000000000000000000000000000";
        //创建界面组件
        List<VexComponents> vexComponents = new ArrayList<>();
        vexComponents.add(new VexText(160,30,Arrays.asList("§5每日签到")));
        vexComponents.add(new VexSlot(0,30,100,new ItemStack(Material.APPLE,1)));
        VexButton vexButton = new VexButton(50,"§5今日未签到","[local]Login\\gui.png","[local]Login\\gui.png",15,130,50,20);
        vexComponents.add(vexButton);
        //访问数据库，查找该玩家是否存在，若不在则加入到数据库
        try{
            //玩家是否存在
            ArrayList<String> strings = SQLiteManager.get().queryPlayerAttendance(player);
            if(strings.size() != 0){
                totalTime = Integer.parseInt(strings.get(0));
                monthRecord = strings.get(1);
                main.plugin.getLogger().info("全部：" + totalTime + " 记录" + monthRecord);
            }else{
                SQLiteManager.get().insertPlayerAttendance(player);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        //处理签到的一系列问题，包括天数，签到与未签到颜色变化等
        LocalDate localDate = LocalDate.now();
        int num = localDate.lengthOfMonth(); //当月的天数
        int day = localDate.getDayOfMonth(); //当月所在天数
        String url1,url2; //第一张和第二张贴图
        main.plugin.getLogger().info("开始签到显示");
        for(int i=1;i<=num;i++){
            int x = 90+(i-1)%6*20*2; //按钮坐标x
            int y = 50+(i-1)/6*20+(i-1)/6*2; //按钮坐标y
            String hover = "未到时间，无法签到";
            url1 = "[local]Login\\D"+i+".png";
            url2 = "[local]Login\\S"+i+".png";
            if(i<day){
                //过去的时间，若是没签到则补签
                url1 = url2 = "[local]Login\\N"+i+".png";
                hover = "时间已过，无法签到";
                if(monthRecord.charAt(i-1) == '0'){
                    url1 = "[local]Login\\Signa.png";
                    hover = "点击补签";
                }else{
                    monthTime++;
                }
            }else if(i==day){
                //现在的时间，若签到无法在签到
                if(monthRecord.charAt(day-1) == '0') {
                    url1 = "[local]Login\\hub.png";
                    hover = "点击签到";
                    vexButton.setName("§5今日未签到");
                }else{
                    url1 = url2 = "[local]Login\\N"+i+".png";
                    hover = "今日已签到";
                    vexButton.setName("§5今日已签到");
                    monthTime++;
                }
            }
            VexButton button = new VexButton(i, "", url1, url2, x, y, 20, 20);
            button.setHover(Arrays.asList(hover));
            vexComponents.add(button);
        }
        //显示相关信息
        vexComponents.add(new VexText(10,50,Arrays.asList("§5累计签到"+totalTime+"天")));
        vexComponents.add(new VexText(10,70,Arrays.asList("§5本月已签到"+monthTime+"天")));
        //创建玩家形象
        vexComponents.add(new VexPlayerDraw(360,120,30,player.getUniqueId(),player.getName()));  //x,y,大小，玩家UUID，玩家称号
        return new VexGui("[local]Login\\login.png",-1,-1,400,200,vexComponents);
    }


    //任务界面
    public static VexGui tasksGui(Player player){
        List<VexComponents> vexComponents = new ArrayList<>();
        vexComponents.add(new VexText(150,10,Arrays.asList("§5任务中心")));
        vexComponents.add(new VexText(20,30,Arrays.asList("§5任务列表")));

        int i = 0;
        Iterator<Task> it_task = TasksTool.list_DailyTask.iterator();
        while(it_task.hasNext()){
            boolean flag = false;
            Task task = it_task.next();
            if(!flag){
                Iterator<PlayerTasks> it_playertasks = TasksTool.list_tasks.iterator();
                while(it_playertasks.hasNext()){
                    PlayerTasks playerTasks = it_playertasks.next();
                    if(playerTasks.getPlayer().getUniqueId() == player.getUniqueId()){
                        Iterator<Task> it_unfinished = playerTasks.getList_task().iterator();
                        while(it_unfinished.hasNext()){
                            Task t = it_unfinished.next();
                            if(t.getID() == task.getID()){
                                //加任务到界面，任务正在进行中
                                int y = 50 + i*30;
                                VexButton button1 = new VexButton(500+i,task.getTaskName(),"[local]Task\\button0.png","[local]Task\\button0.png",20,y,280,25);
                                List<String> hover = new ArrayList<>();
                                hover.add(task.getTaskDescription());
                                hover.add("任务要求：");
                                final HashMap tasks_map = task.getTasks();
                                for(Object key:tasks_map.keySet()){
                                    hover.add(key.toString() + ":" + tasks_map.get(key));
                                }
                                button1.setHover(hover);
                                vexComponents.add(button1);
                                VexButton button2 = new VexButton(task.getID(),"任务中","[local]Task\\bq0.png","[local]Task\\bq0.png",310,y,40,25);
                                button2.setHover(Arrays.asList("点击取消" +task.getTaskName()+ "任务"));
                                vexComponents.add(button2);
                                //player.sendMessage("list_task" + i);
                                flag = true;
                                i++;
                            }
                        }
                        break;
                    }
                }
            }
            if(!flag){
                Iterator<PlayerTasks> it_playertasks2 = TasksTool.list_finished.iterator();
                while(it_playertasks2.hasNext()){
                    PlayerTasks playerTasks = it_playertasks2.next();
                    if(playerTasks.getPlayer().getUniqueId() == player.getUniqueId()){
                        Iterator<Task> it_unfinished = playerTasks.getList_task().iterator();
                        while(it_unfinished.hasNext()){
                            Task t = it_unfinished.next();
                            if(t.getID() == task.getID()&&!flag){
                                //加任务到界面，任务已完成

                                int y = 50 + i*30;
                                VexButton button1 = new VexButton(500+i,task.getTaskName(),"[local]Task\\button0.png","[local]Task\\button0.png",20,y,280,25);
                                List<String> hover = new ArrayList<>();
                                hover.add(task.getTaskDescription());
                                hover.add("任务要求：");
                                final HashMap tasks_map = task.getTasks();
                                for(Object key:tasks_map.keySet()){
                                    hover.add(key.toString() + ":" + tasks_map.get(key));
                                }
                                button1.setHover(hover);
                                vexComponents.add(button1);
                                VexButton button2 = new VexButton(task.getID(),"已完成","[local]Task\\bq0.png","[local]Task\\bq0.png",310,y,40,25);
                                button2.setHover(Arrays.asList("您已完成" +task.getTaskName()+ "任务"));
                                vexComponents.add(button2);
                                flag = true;
                                i++;
                            }
                        }
                        break;
                    }
                }
            }
            if(!flag) {
                //加任务到界面，任务未领取

                int y = 50 + i * 30;
                VexButton button1 = new VexButton(500 + i, task.getTaskName(), "[local]Task\\button0.png", "[local]Task\\button0.png", 20, y, 280, 25);
                List<String> hover = new ArrayList<>();
                hover.add(task.getTaskDescription());
                hover.add("任务要求：");
                final HashMap tasks_map = task.getTasks();
                for (Object key : tasks_map.keySet()) {
                    hover.add(key.toString() + ":" + tasks_map.get(key));
                }
                button1.setHover(hover);
                vexComponents.add(button1);

                //player.sendMessage("daily button1" + button1.getId().toString());
                VexButton button2 = new VexButton(task.getID(), "领取任务", "[local]Task\\bq0.png", "[local]Task\\bq0.png", 310, y, 40, 25);
                button2.setHover(Arrays.asList("点击领取" +task.getTaskName()+ "任务"));
                vexComponents.add(button2);
                //player.sendMessage("daily button2" + button2.getId().toString());
                //flag = true;
                i++;
            }
        }



        return new VexGui("[local]Task\\gui.png",-1,-1,350,200,vexComponents);
    }


    //活跃度界面
    public static VexGui activityGui(){
        List<VexComponents> vexComponents = new ArrayList<>();
        vexComponents.add(new VexText(150,10,Arrays.asList("§5玩家活跃度")));
        vexComponents.add(new VexText(20,40,Arrays.asList("§5本月签到")));
        vexComponents.add(new VexButton(1,"领取奖励","[local]button.png","[local]button_.png",50,70,40,10));
        vexComponents.add(new VexText(20,100,Arrays.asList("§5任务完成情况")));
        vexComponents.add(new VexButton(2,"领取奖励","[local]button.png","[local]button_.png",50,140,40,10));
        return new VexGui("[local]gui.png",-1,-1,350,200,vexComponents);
    }


    //位置传送界面
    public static VexGui transferGui(){
        List<VexComponents> vexComponents = new ArrayList<>();
        vexComponents.add(new VexText(150,10,Arrays.asList("§5传送中心")));

        vexComponents.add(new VexText(10,30,Arrays.asList("§5传输地址列表")));
        vexComponents.add(new VexText(20,50,Arrays.asList("§5地址一：主城下方(x:140 y:81 z:565)")));
        vexComponents.add(new VexText(20,70,Arrays.asList("§5地址二：主城门下(x:100 y:76 z:620)")));
        vexComponents.add(new VexText(20,90,Arrays.asList("§5地址三：主城门口(x:146 y:106 z:591)")));
        vexComponents.add(new VexText(20,110,Arrays.asList("§5地址四：沙漠(x:56 y:74 z:430)")));

        vexComponents.add(new VexText(10,160,Arrays.asList("§5您暂时需要手动输入以完成传送...")));

        vexComponents.add(new VexText(20,180,Arrays.asList("§5x：")));
        vexComponents.add(new VexTextField(30,180,50,10,10,105));
        vexComponents.add(new VexText(100,180,Arrays.asList("§5y：")));
        vexComponents.add(new VexTextField(110,180,50,10,10,106));
        vexComponents.add(new VexText(180,180,Arrays.asList("§5z：")));
        vexComponents.add(new VexTextField(190,180,50,10,10,107));
        vexComponents.add(new VexButton(110,"点击传送","[local]button.png","[local]button_.png",270,180,40,10));
        return new VexGui("[local]gui.png",-1,-1,350,200,vexComponents);
    }


    //个人信息界面
    public static VexGui personalGui(){
        List<VexComponents> vexComponents = new ArrayList<>();
        vexComponents.add(new VexText(150,10,Arrays.asList("§5个人中心")));
        return new VexGui("[local]gui.png",-1,-1,350,200,vexComponents);
    }
    //商城界面
    public static void shopInventory(Player player) {

        new BukkitRunnable() {
            @Override
            public void run() {
                Inventory inventory = Bukkit.createInventory(null, 27, "商店(点击购买)");
                //数据库取得数据，初始化仓库
                ArrayList<Goods> goods = SQLiteManager.get().selectGoods();
                int len = goods.size() <= 27 ? goods.size() : 27;
                main.plugin.getLogger().info("获得数据" + len);
                for (int i = 0; i < len; i++) {
                    ItemStack itemStack = SavaItemStack.getItemStack(goods.get(i).getName());
                    itemStack.setAmount(goods.get(i).getNumber());
                    ItemMeta itemMeta = itemStack.getItemMeta();
                    itemMeta.setLore(Arrays.asList("ID:" + goods.get(i).getId(), "Price:" + goods.get(i).getPrice()));
                    itemStack.setItemMeta(itemMeta);
                    inventory.addItem(itemStack);
                }

                player.openInventory(inventory);
                player.updateInventory();
            }
        }.runTaskAsynchronously(main.plugin);
    }


}
